package com;

import com.Semaphore.SemaphoreTest;

//import com.alibaba.fastjson.JSON;
//import com.logicmonitor.tsdb.commonutils.Settings;
//import com.logicmonitor.tsdb.tsdbcli.impls.zkkafka.ZkKafkaTcpAsyncClient;
import com.google.common.base.Strings;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
//import scala.collection.immutable.Stream;

public class MainTest {

    public void test(int i) {
        int j = 0;
    }

    public static void main(String[] args) throws Exception {

//        RandomAccessFile aFile = new RandomAccessFile("/Users/mike/workspace/git/knowledge/JustTest/src/main/java/com/test", "rw");
//        FileChannel inChannel = aFile.getChannel();
//
//        ByteBuffer buf = ByteBuffer.allocate(48);
//
//
//        buf.put(new Long(128).byteValue());
//        System.out.println(String.format("[capacity:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),buf.limit(),buf.position(),buf.remaining()));
//        buf.flip();
//        while (buf.hasRemaining()){
//            System.out.println(buf.get());
//        }

//        int i = inChannel.read(buf);
//         buf.put(double2Bytes(1.234));
//                    System.out.println(String.format("[capacity:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),buf.limit(),buf.position(),buf.remaining()));
//        buf.flip();
//        byte[] arr = new byte[8];
//        int f=0;
//        while(buf.hasRemaining()) {
//            arr[f] = buf.get();
//            f++;
//        }
//        System.out.println(bytes2Double(arr));
//        int bytesRead = inChannel.read(buf);
//        int i=0;
//        while ((i = inChannel.read(buf)) != -1) {
//
//            System.out.println(String.format("[capacity:%s],[read:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),i,buf.limit(),buf.position(),buf.remaining()));
//            buf.flip();
//            System.out.println(String.format("after flip======>[capacity:%s],[read:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),i,buf.limit(),buf.position(),buf.remaining()));
//
//            while (buf.hasRemaining()) {
////                System.out.print((char) buf.get());
//                buf.get();
//                System.out.println(String.format("after get======>[capacity:%s],[read:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),i,buf.limit(),buf.position(),buf.remaining()));
////                TimeUnit.MILLISECONDS.sleep(500);
//            }
////
//            buf.clear();
//            System.out.println(String.format("after clear======>[capacity:%s],[read:%s],[limit:%s],[position:%s][remain:%s]",buf.capacity(),i,buf.limit(),buf.position(),buf.remaining()));
//            System.out.println("==========================================================================================================================================================================================");
//            TimeUnit.MILLISECONDS.sleep(500);
////            bytesRead = inChannel.read(buf);
//        }
//        aFile.close();

//        ByteBuffer b1 = ByteBuffer.allocate(9);
//        ByteBuffer b2 = ByteBuffer.allocate(10);
//        b1.put("1".getBytes());
//        b2.put("1".getBytes());
//        System.out.println(b1.equals(b2));
//        System.out.println(b2.compareTo(b1));

//        RandomAccessFile aFile = new RandomAccessFile("/Users/mike/workspace/git/knowledge/JustTest/src/main/java/com/test", "rw");
//        FileChannel fileChannel = aFile.getChannel();
//        ByteBuffer b1 = ByteBuffer.allocate(3);
//        ByteBuffer b2 = ByteBuffer.allocate(4);
//        ByteBuffer[] b3 = new ByteBuffer[]{b1,b2};
//        fileChannel.read(b3);
//        System.out.println(String.format("[capacity:%s],[limit:%s],[position:%s][remain:%s]",b1.capacity(),b1.limit(),b1.position(),b1.remaining()));
//        System.out.println(String.format("[capacity:%s],[limit:%s],[position:%s][remain:%s]",b2.capacity(),b2.limit(),b2.position(),b2.remaining()));
//        b1.flip();b2.flip();
//        while(b1.hasRemaining()){
//            System.out.println((char) b1.get());
//        }
//        while(b2.hasRemaining()){
//            System.out.println((char)b2.get());
//        }
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//        Long startTime =format.parse("2019-05-13 09:59:00").getTime();
//        Long endTime =format.parse("2019-05-13 10:09:00").getTime();
//        Long tmp = (endTime-startTime)/(3*60*1000);
//        System.out.println(tmp);
//        System.out.println(endTime+"----"+startTime);
//        Long time =format.parse("2019-05-13 10:00:00").getTime();
//        Long t1 = time%(3*60*1000);
//        System.out.println("t1 = [" + t1 + "]");
//        Bean1 bean1 = new Bean1("mike",1);
//        HashMap<Bean1,String> map = new HashMap<>();
//        map.put(bean1,"1");
//        Bean1 bean2 = new Bean1("mike",1);
//        map.put(bean2,"2");
//        System.out.println(JSON.toJSONString(map));
//        map.remove(bean2);
//        map.remove(bean1);
//        System.out.println(JSON.toJSONString(map));
//        System.out.println(bean1==bean2);

//        Bean2 bean2 = new Bean2();
//        bean2.comp(bean1);
//        System.out.println("args = [" + JSON.toJSONString(bean1) + "]");
//        Bean3<Stringing> bean3 = new Bean3<String>();
//        if(bean3 instanceof Bean3){
//
//        }
//        Bean3<Integer> bean3_1 = new Bean3<>();
//        test(bean3);

//        Bean3<String,String> bean3 = new Bean3<>();
//        bean3.test("fdsa");
//        Integer a = bean3.test2("123",456);
//        System.out.println(a);

//        ZkKafkaTcpAsyncClient client = new ZkKafkaTcpAsyncClient(zkConf, nettyBootsrap, new Settings());

//        Stream.iterate(1L, i -> i + 1).limit(3).reduce(Long::sum);

//        final Properties props = new Properties();
//        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-test");
//        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
//        props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
//        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
//        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

        // setting offset reset to earliest so that we can re-run the demo code with the same pre-loaded data
        // Note: To re-run the demo, you need to use the offset reset tool:
        // https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Streams+Application+Reset+Tool
//        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

//        StreamsBuilder builder = new StreamsBuilder();
//        KStream<Object,Object> ks = builder.stream("test");
//
//        ks.foreach((t1,t2)-> System.out.println(t2.toString()+"...."));
//
//        final KafkaStreams streams = new KafkaStreams(builder.build(), props);
//
//        streams.start();
//
//
//
//        CountDownLatch latch = new CountDownLatch(1);
//        latch.await();














    }

//    public static void test(Bean3<? extends Object> param){
//        System.out.println("param = [" + param.getName() + "]");
//    }


    public static byte[] double2Bytes(double d) {
        long value = Double.doubleToRawLongBits(d);
        byte[] byteRet = new byte[8];
        for (int i = 0; i < 8; i++) {
            byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
        }
        return byteRet;
    }

    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    protected static String secret = "tvnw63ufg9gh5392";
    private static GenericObjectPool<Cipher> cipherPool;
    protected static Base64 coder;
    private static String _ENCODING = "utf8";


    public static byte[] encrypt(byte[] plainText, String secretFactor) throws Exception {
        Cipher cipher = null;

        byte[] var5;
        try {
            SecretKey key = new SecretKeySpec(_generateSecret(secretFactor), "AES");
//            cipher = (Cipher) cipherPool.borrowObject();
            cipher.init(1, key);
            byte[] cipherText = cipher.doFinal(plainText);
            var5 = coder.encode(cipherText);
        } finally {
            if (cipher != null) {
                cipherPool.returnObject(cipher);
            }

        }

        return var5;
    }

    private static byte[] _generateSecret(String secretFactor) {
        if (Strings.isNullOrEmpty(secretFactor)) {
            return secret.getBytes();
        } else {
            if (secretFactor.length() > 15) {
                secretFactor = secretFactor.substring(secretFactor.length() - 15);
            }

            byte[] secretBytes = secret.getBytes();
            byte[] secretFactorBytes = secretFactor.getBytes();

            for (int i = 0; i < secretFactorBytes.length; ++i) {
                secretBytes[i] &= secretFactorBytes[i];
            }

            return secretBytes;
        }
    }
}
